package timing.ukulele.storage.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.storage.model.FileCheckModel;
import timing.ukulele.storage.model.FileCompleteModel;
import timing.ukulele.storage.service.minio.FileService;
import timing.ukulele.storage.vo.file.CompleteResultVO;
import timing.ukulele.storage.vo.file.FileCheckResultVO;

/**
 * 对象存储标准接口定义
 * 本类的方法是给前端使用的方法
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "存储接口")
public class FileController {

    /**
     * 重定向
     */
    private static final String REDIRECT_PREFIX = "redirect:";

    /**
     * 存储引擎Service接口定义
     */
    private final FileService storageService;

    /**
     * 构造方法
     *
     * @param storageService 注入存储引擎Service接口定义实现类
     */
    public FileController(FileService storageService) {
        this.storageService = storageService;
    }


    /**
     * 上传任务初始化
     * 上传前的预检查：秒传、分块上传和断点续传等特性均基于该方法实现
     *
     * @param fileCheckDTO 文件预检查入参
     * @return 检查结果
     */
    @Operation(summary = "上传任务初始化")
    @PostMapping("/upload/init")
    ResponseData<FileCheckResultVO> init(@RequestHeader("account") String userId,
                                         @Parameter(description = "组", name = "team", in = ParameterIn.QUERY) @RequestParam(value = "team", required = false) String team,
                                         @RequestBody @Validated FileCheckModel fileCheckDTO) {
        FileCheckResultVO resultVo = storageService.init(fileCheckDTO.getFileMd5(), fileCheckDTO.getFullFileName(), fileCheckDTO.getFileSize(), userId, team);
        return ResponseData.success(resultVo);
    }

    /**
     * 文件上传完成
     *
     * @param fileKey         文件KEY
     * @param fileCompleteDTO 文件完成入参DTO
     * @return 是否成功
     */
    @Operation(summary = "上传完成")
    @PostMapping("/upload/complete/{fileKey}")
    ResponseData<Object> complete(@RequestHeader("account") String userId, @PathVariable("fileKey") String fileKey,
                                  @Parameter(description = "文件名称", name = "parentId", in = ParameterIn.QUERY) @RequestParam(value = "parentId", required = false) String parentId,
                                  @Parameter(description = "组", name = "team", in = ParameterIn.QUERY) @RequestParam(value = "team", required = false) String team,
                                  @RequestBody FileCompleteModel fileCompleteDTO) {
        // 打印调试日志
        log.debug("合并文件开始fileKey=" + fileKey + ",partMd5List=" + fileCompleteDTO.getPartMd5List());
        CompleteResultVO completeResultVo = storageService.complete(fileKey, fileCompleteDTO.getPartMd5List(), userId, team, parentId);
        return ResponseData.success(completeResultVo);
    }

}